package com.atguigu.gmall.product.service.impl;

import com.atguigu.gmall.common.execption.GmallException;
import com.atguigu.gmall.model.product.SpuImage;
import com.atguigu.gmall.model.product.SpuInfo;
import com.atguigu.gmall.model.product.SpuSaleAttr;
import com.atguigu.gmall.model.product.SpuSaleAttrValue;
import com.atguigu.gmall.product.mapper.SpuImageMapper;
import com.atguigu.gmall.product.mapper.SpuInfoMapper;
import com.atguigu.gmall.product.mapper.SpuSaleAttrMapper;
import com.atguigu.gmall.product.mapper.SpuSaleAttrValueMapper;
import com.atguigu.gmall.product.service.SpuImageService;
import com.atguigu.gmall.product.service.SpuInfoService;
import com.atguigu.gmall.product.service.SpuSaleAttrService;
import com.atguigu.gmall.product.service.SpuSaleAttrValueService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/***
 * spu信息表的service层实现类
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class SpuInfoServiceImpl implements SpuInfoService {

    @Autowired
    private SpuInfoMapper spuInfoMapper;

    @Autowired
    private SpuImageService spuImageService;

    @Autowired
    private SpuSaleAttrService spuSaleAttrService;

    @Autowired
    private SpuImageMapper spuImageMapper;

    @Autowired
    private SpuSaleAttrMapper spuSaleAttrMapper;

    @Autowired
    private SpuSaleAttrValueMapper spuSaleAttrValueMapper;


    /**
     * 根据category3Id分页查询spu信息表数据
     *
     * @param category3Id
     * @param pageNum
     * @param size
     * @return
     */
    @Override
    public IPage<SpuInfo> listPageSpuInfoByCategory3Id(Long category3Id, Long pageNum, Long size) {
        //2 分页擦
        Page<SpuInfo> page = new Page<>(pageNum, size);
        LambdaQueryWrapper<SpuInfo> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SpuInfo::getCategory3Id, category3Id);
        IPage<SpuInfo> spuInfoIPage = spuInfoMapper.selectPage(page, wrapper);

        //3 返回结果
        return spuInfoIPage;
    }

    /**
     * 保存 / 修改spu信息表数据
     *
     * @param spuInfo
     */
    @Override
    public void save(SpuInfo spuInfo) {
        //获取spu信息表id
        Long spuId = spuInfo.getId();

        if (null == spuId) {
            //保存spu信息表数据
            int insert = spuInfoMapper.insert(spuInfo);
            if (insert <= 0) {
                throw new GmallException("新增spu信息表数据失败！", null);
            }
        } else {

            //修改spu信息表的数据
            spuInfoMapper.updateById(spuInfo);
            //删除spu图片表的数据
            int delete = spuImageMapper.delete(
                    new LambdaQueryWrapper<SpuImage>()
                            .eq(SpuImage::getSpuId, spuId));
            //删除squ销售属性表的数据
            int delete1 = spuSaleAttrMapper.delete(
                    new LambdaQueryWrapper<SpuSaleAttr>()
                            .eq(SpuSaleAttr::getSpuId, spuId));
            //删除squ销售属性值表的数据
            int delete2 = spuSaleAttrValueMapper.delete(
                    new LambdaQueryWrapper<SpuSaleAttrValue>()
                            .eq(SpuSaleAttrValue::getSpuId, spuId));
            //判断，都要删除成功
            if (delete < 0 || delete1 < 0 || delete2 < 0) {
                throw new GmallException("修改spu信息表失败！", null);
            }
        }

        //获取spu信息表id
        spuId = spuInfo.getId();

        //保存spu图片表数据
        spuImageService.save(spuId, spuInfo.getSpuImageList());

        //保存销售属性表数据和销售属性值表数据
        spuSaleAttrService.save(spuId, spuInfo.getSpuSaleAttrList());

    }
}
